const httpclient = require('../../utils/httpclient.js')
const md5Util = require('../../utils/md5.js')
const util = require('../../utils/util.js')

var app = getApp();
Page({
    data:{
      orderId:0,
      goodsList:[
      ]
    },
    onLoad:function(e){
      var orderId = e.id;
      var that = this;
      this.setData({
        orderId: orderId
      });
      httpclient.req(
        "/mall/find_order_detail_by_id",
        {
          id: orderId
        },
        'post',
        function (res) {
          that.setData({
            orderDetail: res.data,
            goodsList: res.data.details
          });

          // 如果状态为1代付款要计算当前时间与最后支付时间差
          if (res.data.state == 1) {
            var create_date = new Date(res.data.create_date.replace(/-/g, '/'));
            
            console.log(res.data.create_date);
            console.log(create_date);
            create_date.setTime(create_date.getTime() + 12 * 60 * 60 * 1000);
            var now = new Date();
            var date3 = create_date.getTime() - now.getTime()  //时间差的毫秒数
            //计算出小时数
            var leave1 = date3 % (24 * 3600 * 1000)    //计算天数后剩余的毫秒数
            var hours = Math.floor(leave1 / (3600 * 1000))
            //计算相差分钟数
            var leave2 = leave1 % (3600 * 1000)        //计算小时数后剩余的毫秒数
            var minutes = Math.floor(leave2 / (60 * 1000))
            //计算相差秒数
            var leave3 = leave2 % (60 * 1000)      //计算分钟数后剩余的毫秒数
            var seconds = Math.round(leave3 / 1000)
            that.setData({
              hours: hours,
              minutes: minutes,
              seconds: seconds
            });

            var timer = setInterval(function(){
              var hours = that.data.hours, minutes = that.data.minutes, seconds = that.data.seconds;
              if (that.data.seconds > 0){
                seconds = parseInt(that.data.seconds)-1;
              } else if (that.data.minutes > 0) {
                minutes = parseInt(that.data.minutes) - 1;
                seconds = 59;
              } else if (that.data.hour > 0) {
                if (that.data.hours == 0){
                  clearInterval(timer);
                  // 刷新当前页面
                  that.onShow();
                }
                hours = parseInt(that.data.hours) - 1;
                minutes = 59;
                seconds = 59;
              }

              that.setData({
                hours: hours,
                minutes: minutes,
                seconds: seconds
              });
            },1000);
          }
        },
        function (result) {
          console.log(result)
        }
      );
      /** 
       * 获取系统信息 
       */
      wx.getSystemInfo({
        success: function (res) {
          var n = 750 / res.windowWidth;
          that.setData({
            winWidth: res.windowWidth,
            winHeight: res.windowHeight,
            scrollHeight: res.windowHeight - (87 / n)
          });
        }
      });
    },
    onShow : function () {
      var that = this;

      var yunPrice = parseFloat(this.data.yunPrice);
      var allprice = 0;
      var goodsList = this.data.goodsList;
      for (var i = 0; i < goodsList.length; i++) {
        allprice += parseFloat(goodsList[0].price) * goodsList[0].number;
      }
      this.setData({
        allGoodsPrice: allprice
      });
    },
    wuliuDetailsTap:function(e){
      var orderId = e.currentTarget.dataset.id;
      wx.navigateTo({
        url: "/pages/wuliu/index?id=" + orderId
      })
    },
    toPayTap: function (e) {
      var orderId = e.currentTarget.dataset.id;
      var money = e.currentTarget.dataset.money;
      var prepay_id = e.currentTarget.dataset.prepay_id;

      var that = this;
      console.log(parseInt(money * 100));
      //appid
      let app_id = app.globalData.appId;
      //商户号
      let mch_id = app.globalData.mch_id;
      //时间戳
      let timestamp = (new Date()).valueOf() + '';
      //商户key
      let shop_key = app.globalData.shop_key;
      //随机字符串
      let nonceStr = util.makeRandom(10) + '';
      //签名类型
      let signType = 'MD5';
      //package
      let _package = "prepay_id=" + prepay_id;
      let content = 'appId=' + app_id + '&nonceStr=' + nonceStr + '&package=' + _package
        + '&signType=MD5&timeStamp=' + timestamp + '&key=' + shop_key;

      //调起微信支付
      wx.requestPayment({
        'timeStamp': timestamp,
        'nonceStr': nonceStr,
        'package': _package,
        'signType': 'MD5',
        'paySign': md5Util.md5(content),
        'success': function (res) {
          var pages = getCurrentPages();
          var prevPage = pages[pages.length - 2];  //上一个页面
          let dishesList = prevPage.data.dishesList;
          dishesList.forEach(function (dishes, index) {
            dishes.forEach((dishe, s_index) => {
              dishe.count = 0;
            });
          });
          //直接调用上一个页面的setData()方法，把数据存到上一个页面中去
          prevPage.setData({
            cart: {},
            cartTotal: 0,
            cartPrice: 0,
            dishesList: dishesList
          })

          // 跳转到餐厅首页
          wx.navigateBack({

          });
        },
        'fail': function (res) {
          console.log("接口调用失败的回调函数");
          that.setData({ payFalse: false });
        }
      });
    }
})